﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.ZhiJian
{
    public partial class ZhiLiangShu_MingXi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.GridView1.DataSourceID = this.SqlDataSource1.ID;
            }
        }

        protected void Save_Click(object sender, EventArgs e)
        {
            
            int 质量书ID = int.Parse(Request.QueryString["ID"]);
            string 产品批号 = this.PiHao.Text;
            string 管坯序号 = this.PeHao.Text;
            string 炉号 = this.LuHao.Text;
            if (炉号 == "")
            {
                this.Message.Text = ".炉号不能为空!";
                return;
            }
            string 规格 = this.GuiGe.Text;
            if (规格 == "")
            {
                this.Message.Text = ".规格不能为空!";
                return;
            }

            string 长度 = this.ChangDu.Text;
            if (this.捆数.Text == "")
            {
                this.Message.Text = ".捆数不能为空!";
                return;
            }
            if (this.ZhiShu.Text == "")
            {
                this.Message.Text = ".支数不能为空!";
                return;
            }
            if (this.ZhongLiang.Text == "")
            {
                this.Message.Text = ".重量不能为空!";
                return;
            }
            int 捆数 = int.Parse(this.捆数.Text);
            int 支数 = int.Parse(this.ZhiShu.Text);
            decimal 重量 = decimal.Parse(this.ZhongLiang.Text);
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            string strSql = @"INSERT INTO [SCM].[dbo].[质量书明细]
           ([质量书ID]
           ,[产品批号]
           ,[管坯序号]
           ,[炉号]
           ,[规格]
           ,[长度]
           ,[捆数]
           ,[支数]
           ,[重量])
           VALUES
           ('" + 质量书ID + "','" + 产品批号 + "','" + 管坯序号 + "','" + 炉号 + "','" + 规格 + "','" + 长度 + "','" + 捆数 + "','" + 支数 + "','" + 重量 + "')";
            SqlCommand cmd = new SqlCommand(strSql, Conn);
            try
            {

                int i = cmd.ExecuteNonQuery();
                if (i > 0)
                {
                    this.Message.Text = ".增加成功";
                }
                else
                {
                    this.Message.Text = ".增加有误，请检查后在添加";                   
                }

            }
            catch (Exception ex)
            {
                this.Message.Text = ex.Message.ToString();
            }
            finally
            {
                cmd.Clone();
                Conn.Close();
            }
            this.GridView1.DataBind();
            this.Panel2.Visible = false;
            Response.Redirect("ZhiLiangShu_MingXi.aspx?ID=" + 质量书ID);
        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {
                this.SqlDataSource1.DeleteCommand = "Exec ZhiLiangShu_MingXi_Delete " + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
                this.SqlDataSource1.DeleteCommandType = SqlDataSourceCommandType.Text;
                this.SqlDataSource1.Delete();              
                this.GridView1.DataSourceID = this.SqlDataSource1.ID;
                this.Message.Text = "·删除成功";
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);

        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }       
        //送检操作
        //protected void Button2_Click(object sender, EventArgs e)
        //{
        //    for (int i = 0; i < GridView1.Rows.Count; i++)
        //    {
        //        CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[10].Controls[1];
        //        string ID = this.GridView1.Rows[i].Cells[1].Text;
        //        if (cb.Checked == true)
        //        {
        //            this.SqlDataSource1.UpdateCommand = "Exec ZhiLiangShu_MingXi_SongJian_Update " + "true," +ID;
        //            this.SqlDataSource1.UpdateCommandType = SqlDataSourceCommandType.Text;
        //            this.SqlDataSource1.Update();
        //            this.GridView1.DataSourceID = this.SqlDataSource1.ID;
        //            this.Message.Text = "·送检信息保存成功";
        //        }
        //        else if(cb.Checked == false)
        //        {
        //            this.SqlDataSource1.UpdateCommand = "Exec ZhiLiangShu_MingXi_SongJian_Update " + "false," + ID;
        //            this.SqlDataSource1.UpdateCommandType = SqlDataSourceCommandType.Text;
        //            this.SqlDataSource1.Update();
        //            this.GridView1.DataSourceID = this.SqlDataSource1.ID;
        //            this.Message.Text = "·送检信息保存成功";
        //        }
        //    }          
            
        //}     

        protected void LinkButton_Click(object sender, EventArgs e)
        {
            string 参数 = ((LinkButton)sender).CommandName;
            string[] Args = 参数.Split("\t".ToCharArray());
            Response.Redirect("ZhiLiangShu_MingXi_ToZhiJian.aspx?luhao=" + Args[1] + "&&id=" + Args[0]);          
                        
        }

        protected void Add_Click(object sender, EventArgs e)
        {
            this.Panel2.Visible = true;
        }    
    }
}
